
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulebase.entity.ExamScheduleApm;
import com.rt.schedulebase.mapper.ExamScheduleApmMapper;
import com.rt.schedulenew.api.IExamScheduleApmService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ExamScheduleApmServiceImpl extends ServiceImpl<ExamScheduleApmMapper, ExamScheduleApm> implements IExamScheduleApmService {
    @Override
    public ExamScheduleApm getExamScheduleApm( ExamScheduleApm dto) {
         QueryWrapper<ExamScheduleApm> esaQw = new QueryWrapper<>();
        esaQw.select("COUNT", "ORGANS", "ORGANS_RECYCLE", "COUNT_RECYCLE");
        esaQw.eq("APM_CODE", dto.getApmCode());
        esaQw.eq("SCHEDULE_DATE", dto.getScheduleDate());
         ExamScheduleApm esa = baseMapper.selectOne(esaQw);
        return esa;
    }

    @Override
    public int updateExamScheduleApm( ExamScheduleApm dto) {
         UpdateWrapper<ExamScheduleApm> updateWrapper = new UpdateWrapper<>();
         ExamScheduleApm examScheduleApm = new ExamScheduleApm();
        examScheduleApm.setOrgans(dto.getOrgans());
        examScheduleApm.setCount(dto.getCount());
        if (dto.getCountRecycle() != null) {
            examScheduleApm.setCountRecycle(dto.getCountRecycle());
        }
        if (dto.getOrgansRecycle() != null) {
            examScheduleApm.setOrgansRecycle(dto.getOrgansRecycle());
        }
        updateWrapper.eq("SCHEDULE_DATE", dto.getScheduleDate());
        updateWrapper.eq("APM_CODE", dto.getApmCode());
        return baseMapper.update(examScheduleApm, updateWrapper);
    }

    @Override
    public int insertExamScheduleApm( ExamScheduleApm dto) {
         ExamScheduleApm examScheduleApm = new ExamScheduleApm();
        examScheduleApm.setScheduleDate(dto.getScheduleDate());
        examScheduleApm.setApmCode(dto.getApmCode());
        examScheduleApm.setOrgans(dto.getOrgans());
        examScheduleApm.setCount(dto.getCount());
        return baseMapper.insert(examScheduleApm);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public int updateApmNotRolledBack( ExamScheduleApm dto) {
         UpdateWrapper<ExamScheduleApm> updateWrapper = new UpdateWrapper<>();
         ExamScheduleApm examScheduleApm = new ExamScheduleApm();
        examScheduleApm.setOrgans(dto.getOrgans());
        examScheduleApm.setCount(dto.getCount());
        if (dto.getCountRecycle() != null) {
            examScheduleApm.setCountRecycle(dto.getCountRecycle());
        }
        if (dto.getOrgansRecycle() != null) {
            examScheduleApm.setOrgansRecycle(dto.getOrgansRecycle());
        }
        updateWrapper.eq("SCHEDULE_DATE", dto.getScheduleDate());
        updateWrapper.eq("APM_CODE", dto.getApmCode());
        return (baseMapper).update(examScheduleApm, updateWrapper);
    }
}
